using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Alicanto.Helpers
{
    internal class Typing
    {
        private static List<Type> NUMBER_PRIORITY = new List<Type>
        {
            typeof(ulong),
            typeof(double),
            typeof(long),
            typeof(uint),
            typeof(float),
            typeof(int),
            typeof(ushort),
            typeof(short),
            typeof(sbyte),
            typeof(byte)
        };

        private static Dictionary<string, Type> STD = new Dictionary<string, Type>
        {
            { "int", typeof(int) },
            { "string", typeof(string) }
        };

        internal static bool IsNumber(Type type)
        {
            return type.IsPrimitive && type != typeof(char);
        }

        internal static bool IsAssignableFrom(Type type, Type from)
        {
            if (type.IsAssignableFrom(from))
                return true;

            if (IsNumber(type))
                return true;

            return false;
        }

        internal static Type ResolveNumberConversion(Type x, Type y)
        {
            return (NUMBER_PRIORITY.IndexOf(x) > NUMBER_PRIORITY.IndexOf(y)) ? x : y;
        }

        internal static Type FromString(string typeName)
        {
            Type result = Type.GetType(typeName);
            if (result != null)
                return result;

            if (STD.ContainsKey(typeName))
                return STD[typeName];

            return null;
        }
    }
}
